package com.sikaryofficial.im;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;


/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-11-05
 */


public class DiscardServer {

	static class DiscardServerHandler extends ChannelInboundHandlerAdapter {

		@Override
		public void channelRead(ChannelHandlerContext ctx, Object msg) {
			// 默默地丢弃收到的数据
		}

		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
			// 关闭通道
			ctx.close();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		ServerBootstrap bootstrap = new ServerBootstrap();
		// 1. 配置反应器轮询组
		EventLoopGroup bossGroup = new NioEventLoopGroup(1);
		EventLoopGroup workerGroup = new NioEventLoopGroup(5);
		bootstrap.group(bossGroup, workerGroup);
		// 2. 配置通道的io类型
		bootstrap.channel(NioServerSocketChannel.class);
		// 3. 配置通道的监听端口
		bootstrap.localAddress(8888);
		// 4. 设置通道的配置选项
		bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
		bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
		// 5. 装配子通道的pipeline
		bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

			protected void initChannel(SocketChannel ch) throws Exception {
				ch.pipeline().addLast(new DiscardServerHandler());
			}

		});
		// 6. 绑定端口并启动服务
		ChannelFuture channelFuture = bootstrap.bind().sync();
		channelFuture.channel().localAddress();
		ChannelFuture closeFuture = channelFuture.channel().closeFuture();
		closeFuture.sync();
		bossGroup.shutdownGracefully();
		workerGroup.shutdownGracefully();
	}

}
